<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Weixin\Model;
use Think\Model;

/**
 * 文档基础模型
 */
class LoanModel extends Model{

    /* 用户模型自动完成 */
    protected $_auto = array(
        array('status', 1, self::MODEL_INSERT),
    );
    protected function order_id(){
        //算出订单ID
        $id = date('Ymd').substr(time(),-5).substr(microtime(),2,2).rand(100,999);
        if($this->where("id={$id}")->count())  //订单号已存在
            return $this->order_id();
        else
            return $id;
    }
    public function apply($data){
        $id= $this->order_id();
        $data2['id']        = $id;
        $data2['uid']       = $data['uid'];
        $data2['name']      = $data['name'];
        $data2['money']     = $data['money'];
        $data2['payable']   = $data['payable'];
        $data2['repayment'] = $data['repayment'];
        $data2['day']       = $data['day'];
        $data2['vetting1']  = $data['vetting1'];
        $data2['accrual']   = $data['accrual'];
        $data2['manag']     = $data['manag'];
        $data2['create_time']   = NOW_TIME;
        $data2['loan_time']     = 0;
        $data2['expire_time']   = 0;
        $data2['huankuan_time'] = 0;
        $data2['status']        = 0;
        $model                  = M('Loan');//print_r($data2);exit;
        $id2                     = $model->add($data2);
        if(!empty($id2)){
            $LoanLog =  M('LoanLog');
            $logData = [
                'uid'           => $data2['uid'],
                'loan_id'       => $id,
                'content'       => '',
                'create_time'   => NOW_TIME,
                'status'        => 0,
            ];
            $LoanLog->add($logData);
            return $id;
        }else{
            return false;
        }
    }
    /*
     *   现在借款提示    */
    public function tishi($uid){
        $vo = M('Loan')->where("status<>-1 AND status<>3 AND status<>6 AND uid={$uid}")->find();

        if(empty($vo)){
            return ['status'=>true,'info'=>''];
        }elseif($vo['status']==0){
            // if($vo['jiaoyi']==0){
            //     return ['status'=>false,'info'=>'你已有借款正在申请中，请先缴纳27元审核费','id'=>$vo['id']];
            // }else{
                return ['status'=>false,'info'=>'你已有借款正在申请中，无法继续申请，去看看','url'=>U('agreement?id='.$vo['id'])];
            // }
            
        }elseif($vo['status']==1){
            return ['status'=>false,'info'=>'你已有借款正在待审核，无法继续申请，去看看','url'=>U('loanview?id='.$vo['id'])];
        }elseif($vo['status']==2){
            return ['status'=>false,'info'=>'你已有借款正在审核，无法继续申请，去看看','url'=>U('loanview?id='.$vo['id'])];
        }elseif($vo['status']==4){
            return ['status'=>false,'info'=>'你已有借款已通过正在打款中，无法继续申请，去看看','url'=>U('loanview?id='.$vo['id'])];
        }elseif($vo['status']==5){
            return ['status'=>false,'info'=>'你有未还款暂不能继续申请，无法继续申请,去看看','url'=>U('loanview?id='.$vo['id'])];
        }
        return ['status'=>false,'info'=>'你有未还款暂不能断续申请'];
    }


    /*
     *       提醒
     * */
    public function tixing(){
        $model =  M('Loan');
        $where = array();
        $where['status']        = 5;
        $where['expire_time']   = 5;//4.到期前一天通知  5.还款日提醒（分5条 早9点开始 每 两小时一条 晚上9点截止）     7.逾期则每天通知 永久发送（分5条 早9点开始 每 两小时一条 晚上9点截止）
        $where['lixi_time']     = 0;
        $volist    = $model -> where($where)->select();
        foreach($volist as $val){

        }

    }

}
